package com.vis.mybatisflex;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson2.JSONObject;
import com.vis.mybatisflex.domain.excel.CcHighExcel;
import com.vis.mybatisflex.domain.excel.DemoData;
import com.vis.mybatisflex.service.CcExcelServiceV02;
import com.vis.mybatisflex.util.SpringKit;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;

@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);

        log.info("============开始处理数据==============");

        //TODO 先指定 Service版本
        CcExcelServiceV02 service = SpringKit.getBean(CcExcelServiceV02.class);

        //解析数据，转存数据
        service._1_ReadWrite1();

        //设置数据
        service._2_SetData();

        //不需要拆分的数据
        service._3_NotSplit();

        //进一步区分，可以拆 和 不可以拆
        service._3_2_ErrorAndSplit();

        //处理可以拆分的数据
        service._4_Split();

        //聚合：不拆的 拆失败的 拆完的
        service._5_sum();

        log.info("============数据处理完毕==============");

        //
        //        System.out.println("数据解析完成");

        //        Demo id = demoMapper.selectColumnById("10", "id");
        //        Demo demo = new Demo();
        //        String id1 = IdKit.getId();
        //
        //        demo.setId(id1);
        //
        //        demoMapper.insert(demo);
        //        Demo id22 = demoMapper.selectColumnById(id1, "id");
        //
        //        System.out.println(id);
        //        System.out.println(id22);
        //        String jsonString = JSONObject.toJSONString(id22);
        //        System.out.println(jsonString);
        //        System.out.println();


        //        importExcel();
        //        exportExcel();
        //        getHighList();
    }


    /**
     * 获取高频服务事项名称
     * 一共362条
     */
    public static void getHighList() throws FileNotFoundException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\ccexcel\\政务高频事项库V0.1.xlsx");
        System.out.println(file.getAbsolutePath());
        HashSet<String> allHighName = new HashSet<>();


        // 这里默认读取第一个sheet
        EasyExcel.read(new FileInputStream(file), CcHighExcel.class, new ReadListener<CcHighExcel>() {
            @Override
            public void invoke(CcHighExcel data, AnalysisContext context) {
                log.info("data : {}", JSONObject.toJSONString(data));
                String ywSxName = data.getYwSxName();
                log.info("ywSxName : {}", ywSxName);
                allHighName.add(ywSxName);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                log.info("数据解析完成:一共 {} 条", allHighName.size());

                //继续处理，大表数据
                for (String highName : allHighName) {


                }
                File file = new File("C:\\Users\\Administrator\\Desktop\\ccexcel\\政务完整事项库V0.1.xlsx");


            }
        }).sheet().doRead();

    }


    /**
     * 导入数据
     */
    public static void importExcel() throws FileNotFoundException {

        File file = new File("C:\\Users\\Administrator\\Desktop\\excel\\demo.xlsx");
        System.out.println(file.getAbsolutePath());
        // 这里默认读取第一个sheet
        EasyExcel.read(new FileInputStream(file), DemoData.class, new ReadListener<DemoData>() {
            @Override
            public void invoke(DemoData data, AnalysisContext context) {
                log.info("data : {}", JSONObject.toJSONString(data));
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {

            }
        }).sheet().doRead();

    }


    /**
     * 导出数据
     */
    @SneakyThrows
    public static void exportExcel() {
        String path = "C:\\Users\\Administrator\\Desktop\\ccexcel\\" + System.currentTimeMillis() + ".xlsx";
        ArrayList<DemoData> objects = new ArrayList<>();

        DemoData demoData1 = new DemoData();
        demoData1.setId("111");
        demoData1.setName("222");
        DemoData demoData2 = new DemoData();
        DemoData demoData3 = new DemoData();
        demoData3.setId("8989");
        demoData3.setName("6363");

        objects.add(demoData1);
        objects.add(demoData2);
        objects.add(demoData3);
        File file = new File(path);
        boolean newFile = file.createNewFile();
        EasyExcel.write(path, DemoData.class).sheet("模板").doWrite(objects);

    }


}
